package com.mengdie.shuidi.manager;

import android.content.Context;
import android.content.SharedPreferences;

import com.mengdie.shuidi.AppContext;

/**
 * Created by apple on 16/10/15.
 */

public class MDAccountMgr {

    private static MDAccountMgr instance;


    private String loginName;

    private String loginPwd;

    private boolean isPassowrd;

    private String sessionId;

    private boolean login;
    private String deviceToken;//友盟 token

    public String getDeviceToken() {
        return deviceToken;
    }

    public void setDeviceToken(String deviceToken) {
        this.deviceToken = deviceToken;
    }

    public static MDAccountMgr getInstance() {
        if (instance == null) {
            synchronized (MDAccountMgr.class) {
                instance = new MDAccountMgr();
            }
        }
        return instance;
    }

    private MDAccountMgr() {
        getCache(AppContext.getInstance());
    }

    public void clearCache(Context context) {
        SharedPreferences settings = context.getSharedPreferences(MDConstant.LOGIN_INFO, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.apply();
    }

    public void clearAccountInfo() {
        loginName = "";
        loginPwd = "";
        isPassowrd = false;
        sessionId = "";
        login = false;
        clearCache(AppContext.getInstance());
    }


    public void clearAccountInfoV2() {
        loginName = "";
        loginPwd = "";
        isPassowrd = false;
        SharedPreferences settings = AppContext.getInstance().getSharedPreferences(MDConstant.LOGIN_INFO, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(MDConstant.LOGIN_NAME, loginName);
        editor.putString(MDConstant.LOGIN_PASSWORD, loginPwd);
        editor.putBoolean(MDConstant.IS_PASSWORD, isPassowrd);
        editor.apply();
    }


    public void writeToCache(Context context) {
        SharedPreferences settings = context.getSharedPreferences(MDConstant.LOGIN_INFO, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(MDConstant.LOGIN_NAME, loginName);
        editor.putString(MDConstant.LOGIN_PASSWORD, loginPwd);
        editor.putString(MDConstant.SESSION_ID, sessionId);
        editor.putBoolean(MDConstant.IS_PASSWORD, isPassowrd);
        editor.putBoolean(MDConstant.IS_LOGIN, login);
        editor.putString(MDConstant.DEVICE_TOKEN,deviceToken);

        editor.apply();
    }

    public void getCache(Context context) {
        SharedPreferences sharedata = context.getSharedPreferences(MDConstant.LOGIN_INFO, 0);
        loginName = sharedata.getString(MDConstant.LOGIN_NAME, null);
        loginPwd = sharedata.getString(MDConstant.LOGIN_PASSWORD, null);
        sessionId = sharedata.getString(MDConstant.SESSION_ID, null);
        login = sharedata.getBoolean(MDConstant.IS_LOGIN, false);
        isPassowrd = sharedata.getBoolean(MDConstant.IS_PASSWORD, false);
        deviceToken = sharedata.getString(MDConstant.DEVICE_TOKEN,"");
    }


    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getLoginPwd() {
        return loginPwd;
    }

    public void setLoginPwd(String loginPwd) {
        this.loginPwd = loginPwd;
    }

    public boolean isPassowrd() {
        return isPassowrd;
    }

    public void setPassowrd(boolean passowrd) {
        isPassowrd = passowrd;
    }

    public boolean isLogin() {
        return login;
    }

    public void setLogin(boolean login) {
        this.login = login;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
        SharedPreferences settings = AppContext.getInstance().getSharedPreferences(MDConstant.LOGIN_INFO, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(MDConstant.SESSION_ID, sessionId);
        editor.apply();
    }
}
